<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($CONFIG->adminUrl, 'Home', $CONFIG->imageUrl.'/admin/icons/home.png');

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Geshi loaded');

if(get('change-language')!=''){
    $lang = $DB->escape(get('change-language'));
    if($DB->countRows($CONFIG->dbprefix.'languages', "is_active = 1 AND keyname = '{$lang}'")>0){
        $USER->lang = $lang;
    }else{
        JFX::addError($this->lang('invalid_language'));
    }
    JFX::redirect($CONFIG->adminUrl);
}

if(get('change-workspace')!=''){
    $wsid = (int) get('change-workspace');
    if($DB->countRows($CONFIG->dbprefix.'admin_users_to_workspaces', "user_id = '{$USER->userid}' AND workspace_id = '{$wsid}'")>0){
        $USER->workspace = $wsid;
    }else{
        JFX::addError($this->lang('nonpermitted_workspace'));
    }
    JFX::redirect($CONFIG->adminUrl);
}

if(get('logout')=='1'){
    $USER->logout();
    JFX::addSuccess($this->lang('logout_success'));
    JFX::redirect($CONFIG->adminUrl);
}



global $pageid;

if(get('page')!=''){
    $pageid = (int) get('page');
    $SMARTY->assign('pageid', $pageid);
}

// lets check if they have cookies enabled
if(!isset($_SESSION['JFXAdmin'])){
    if(get('session')==1){
        // cookies not enabled.
        JFX::addError($this->lang('please_enable_cookies'));
        $_SESSION['JFXAdmin'] = 1;
    }else{
        $_SESSION['JFXAdmin'] = 1;
        $requestUri = explode('?', $_SERVER['REQUEST_URI']);
        JFX::redirect($requestUri[0].'/?session=1');
    }
}



$_LEFTCOL_MENU = (string) $THEME->getMainMenu();
$SMARTY->assign('_LEFTCOL_MENU', $_LEFTCOL_MENU);


$eParams = array();



$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before hook JFX_Core.adminPre');

JFX::hook('JFX_Core.adminPre', $eParams);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After hook JFX_Core.adminPre');

include($this->dirname.'/actions/ajax/pages-menu.php');

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After actions/ajax/pages-menu');

// check that ie6 is not trying to access our modern code..
$browser = $_SERVER['HTTP_USER_AGENT'];
$browser = explode(';', $browser);
$browserName = preg_replace('/[^a-zA-Z]/', '', trim($browser[1]));
$browserVersion = preg_replace('/[^0-9\.]/', '', trim($browser[1]));

if($browserName == 'MSIE' && $browserVersion < 7){
    JFX::addError('Your browser "'.$browserName.' '.$browserVersion.'" does not support the technologies used in this application');
    $VIEW->template = 'admin-ie6.tpl';

    $VIEW->addCss('
table{
    width: 60%;
margin: auto;
border: solid 1px pink;
}
table td{
padding: 16px;
vertical-align: middle;
text-align: center;
border: solid 1px red;
background-color: white;
}
table td a{
color: brown;
}
table td img{
height: 200px;
}
');

    return;
}
// end testing for ie6


$VIEW->template = 'admin.tpl';
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/tabbertabs/tabbertabs.js');
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/phpjs/echo.js');
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/phpjs/var_dump.js');
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/jfx/functions.js');
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/jfx/log.js');
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/jfx/admin.js');
$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/tiny_mce/tiny_mce.js');



if(get('get-login-status')==1){
    $rsp = array();
    $rsp = JFX::arrayToObject($rsp);
    if($USER->isLoggedIn() && $USER->isAdmin()){
        $rsp->status = 'ok';
    }else{
        $rsp->status = 'error';
    }
    while(ob_get_level()>0) ob_end_clean();
    die(json_encode($rsp));
}





if(!$USER->isLoggedIn() || !$USER->isAdmin() || get('force-login')==1){
    // show the login form
    include($this->dirname.'/actions/admin-login.php');
}else{
    // the user is logged in and has access to the admin panel

    $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After checking admin permissions');

    //require('admin-actions/license.php');
    $dbParams = array(
        'user_id' => $USER->userid,
        'tstamp' => time(),
        'url' => THIS_PAGE_URL,
        'get_data' => serialize($_GET),
        'post_data' => serialize($_POST),
        'session_data' => serialize($_SESSION),
        'cookie_data' => serialize($_COOKIE),
        'server_data' => serialize($_SERVER)
    );

    $DB->insert($CONFIG->dbprefix.'admin_history', $dbParams);

    // make sure they are in a workspace
    if($USER->workspace == 0){
        $workspaceId = (int) $DB->oneValue($CONFIG->dbprefix.'admin_users_to_workspaces', 'workspace_id', "user_id = '{$USER->userid}' AND is_default = 1");
        $USER->workspace = $workspaceId;
    }

    if($USER->workspace == 0){
        echo $this->lang('no_workspace');
        return;
    }

    $SMARTY->assign('workspace', $DB->oneValue($CONFIG->dbprefix.'workspaces', 'title', "id = '{$USER->workspace}'"));
    





    // find out what has been requested
    $requestUri = explode('?', THIS_PAGE_URL);
    $requestUri = trim($requestUri[0], '/');
    $requestUri = explode('/', $requestUri);
    while($requestUri[0] != trim($CONFIG->adminFolder, '/')){
        unset($requestUri[0]);
        $requestUri = array_regenerate_keys($requestUri);
    };
    unset($requestUri[0]);
    $requestUri = array_regenerate_keys($requestUri);
    $folders = $requestUri;
    $requestUri = implode('/', $requestUri);


    if(array_key_exists(0, $folders)) $action = $folders[0];
    else $action = 'default';

 
    $eParams = array('action'=>$action);

	$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before JFX_Core.adminContentPreRender');

    JFX::hook('JFX_Core.adminContentPreRender', $eParams);

	$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After JFX_Core.adminContentPreRender');

    $realEParams = $eParams;

	
    if(file_exists($this->dirname.'/actions/'.$eParams['action'].'.php')){
		$includeFile = $this->dirname.'/actions/'.$eParams['action'].'.php';
	}else{
		$includeFile = $this->dirname.'/actions/default.php';
	}
	
	$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before include action file '.$includeFile);

	include($includeFile);

	$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After include action file '.$includeFile);

    JFX::hook('JFX_Core.adminContentPostRender', $realEParams);

	$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After JFX_Core.adminContentPostRender');

    //$THEME->install();

	





}



$_BREADCRUMBS = (string) $THEME->getBreadcrumbs();
$SMARTY->assign('_BREADCRUMBS', $_BREADCRUMBS);


if(get('JFXGetAjaxContent')=='1'){
    $VIEW->template = 'admin-ajax-content.tpl';
}

$eParams = array();

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before hook JFX_Core.adminPost');

JFX::hook('JFX_Core.adminPost', $eParams);

$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After hook JFX_Core.adminPost');

